<?php

namespace apiend\modules\dynamic\models;

use Yii;
use yii\data\ActiveDataProvider;
use common\models\user\UserFollower;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class DynamicConcernedSearch extends \common\logics\DynamicLogic {

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($uid) {
        $query = static::find()
        ->alias('t')
        ->joinWith('concerner concerner')
        ->select('t.*')
        ->where([
            't.status' => static::STATUS_ACTIVE,
            't.deleted_at' => 0,
            'concerner.user_id' => $uid
        ]);
        $query->with(['user', 'stats', 'gallery']);

        $query->orderBy(['t.created_at' => SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        Yii::$app->db->cache(function($db) use($dataProvider) {
            $dataProvider->prepare();
        }, 60);

        return $dataProvider;
    }

    public function getConcerner() {
        return $this->hasOne(UserFollower::className(), ['followed_user_id' => 'user_id']);
    }

    public function fields() {
        return [
            'id',
            'format',
            'text' => 'content',
            'pics',
            'video',
            'article',
            'time' => 'created_at',
            'tools',
            'liked',
            'collected',
            'user',
            'redirect',
            'retweet',
        ];
    }

}
